import java.util.concurrent.CountDownLatch;

/**
 * @author gao
 * @date 2025/8/5 0:27
 */

public class Demo5 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(8);
        for (int i = 0; i < 8; i++) {
            int id = i;
            Thread t = new Thread(() -> {
                // 通过sleep模拟
                try {
                    Thread.sleep(1_000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程" + id + "执行完毕");
                // 计数器减一，相当于一个运动员到达终点
                latch.countDown();
            });
            t.start();
        }
        // 主线程通过await()方法等待所有线程结束
        latch.await();
        System.out.println("所有任务执行完毕");
    }
}